package pt.iscte.pcd.project.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;

import pt.iscte.pcd.project.recursoPartilhado.ListaDeFicheiros;


public class Server {

	public static final int PORT = 8080;
	private static final String FILE_SERVER_ROOT = "root";
	private ListaDeFicheiros listFiles = new ListaDeFicheiros(FILE_SERVER_ROOT);
	private LinkedList<DealWithClient> onlineClients = new LinkedList<>();
	
	
	private void startListening() {
		ServerSocket serverSocket = null;
		try {

			serverSocket = new ServerSocket(PORT);
			System.out.println("Server Started:" + serverSocket.toString() + " PORT - " + PORT);
			
			while (true) {
				Socket socketNewClient = serverSocket.accept();
				System.out.println("New Client: " + socketNewClient.toString());
				DealWithClient client = new DealWithClient(socketNewClient, listFiles,getOnlineClients());
				getOnlineClients().add(client);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} 
		finally{
			try {
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
public static void main(String[] args) {
		new Server().startListening();
	}
public LinkedList<DealWithClient> getOnlineClients() {
	return onlineClients;
}
public void setOnlineClients(LinkedList<DealWithClient> onlineClients) {
	this.onlineClients = onlineClients;
}
}
